const Goods = require('../model/goods.model');
class GoodsService {
  async getGoods(goodsname) {
    const res = await Goods.findOne({ where: { goodsname } });
    return res;
  }

  async goodsAdd(goods) {
    const res = await Goods.create(goods);
    return res.dataValues;
  }


  async goodsUpdate(goods) {
    const res = await Goods.update(goods, { where: { id: goods.id } });
    return res[0] > 0;
  }

  async goodsDelete(id) {
    const res = await Goods.destroy({ where: { id } });
    return res > 0;
  }

  async goodsDetail(id) {
    const res = await Goods.findByPk(id);
    return res ? res.dataValues : null;
  }

  async goodsList(page = 1, pageSize = 10, otherGoodsOptions = {}) {
    const offset = (page - 1) * pageSize
    const options = {
      offset,
      limit: pageSize,
    };
    if (otherGoodsOptions.goodsname != '' && otherGoodsOptions.goodsname != null && otherGoodsOptions.goodsname) {
      options.where = {
        goodsname: otherGoodsOptions.goodsname // 根据 goodsname 进行查询
      };
    }

    const res = await Goods.findAll(options);
    const total = await Goods.count();
    console.log(total, 'totaltotal');
    return {
      res, total
    };
  }
}
module.exports = new GoodsService();